package com.scheduled.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

/**
 * @author: create by God of SJF
 * @version: v1.0
 * @description: com.scheduled.config
 * @date:2021/8/31
 */

//@Configuration
public class BatchConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;


    @Bean
    public Job myJob() {
        return jobBuilderFactory.get("myJob").start(flow()).split(new SimpleAsyncTaskExecutor()).add(flow2()).end().build();
    }

    @Bean
    public Step myStep() {
        return stepBuilderFactory.get("myStep").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("哈哈");
                return null;
            }
        }).build();
    }


    @Bean
    public Step myStep5() {
        return stepBuilderFactory.get("myStep5").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("哈哈");
                return null;
            }
        }).build();
    }


    @Bean
    public Step myStep2() {
        return stepBuilderFactory.get("myStep2").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("哈哈");
                return null;
            }
        }).build();
    }

    @Bean
    public Step myStep3() {
        return stepBuilderFactory.get("myStep3").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("哈哈");
                return null;
            }
        }).build();
    }

    @Bean
    public Step myStep4() {
        return stepBuilderFactory.get("myStep4").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("哈哈");
                return null;
            }
        }).build();
    }

    @Bean
    public Flow flow() {
        return new FlowBuilder<Flow>("flow").start(myStep()).build();
    }

    @Bean
    public Flow flow2() {
        return new FlowBuilder<Flow>("flow2").start(myStep2())
                .next(myStep2()).next(myStep3())
                .build();
    }
}
